<?php


namespace app\cms\admin\controller;


use app\cms\model\userGroupModel;
use app\cms\model\userRoleModel;
use iflow\auth\Authorize\AuthAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping, DeleteMapping };

#[Controller('%cmsAdmin%/user_group')]
class userGroup {

    /**
     * 获取 用户组列表
     * @param string $groupName
     * @param int $user_group_type
     * @return Response
     */
    #[GetMapping]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function getUserGroupList(
        string $groupName,
        int $user_group_type
    ): Response {
        return (new userGroupModel()) -> getUserGroupList($groupName, $user_group_type);
    }

    /**
     * 添加用户组
     * @param string $user_group_name
     * @param int $user_group_parent_id
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function addUserGroup(
        string $user_group_name = "",
        int $user_group_parent_id = 0
    ): Response {
        return (new userGroupModel()) -> addUserGroup($user_group_name, $user_group_parent_id);
    }

    /**
     * 更新用户组
     * @param string $user_group_name
     * @param int $user_group_id
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function updateUserGroup(string $user_group_name, int $user_group_id): Response {
        return (new userGroupModel()) -> updateUserGroup($user_group_name, $user_group_id);
    }

    /**
     * 删除用户组
     * @param int $user_group_id
     * @return Response
     */
    #[DeleteMapping('delete_user_group/<[0-9]{1,8}:user_group_id>')]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function deleteUserGroup(int $user_group_id = 0): Response {
        return (new userGroupModel) -> deleteGroup($user_group_id);
    }

    /**
     * 获取指定用户组权限
     * @param int $user_group_id
     * @return Response
     */
    #[GetMapping('get_user_group_role/<[0-9]{1,8}:user_group_id>')]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupRead')]
    public function getUserGroupRole(int $user_group_id): Response {
        $userInfo = (new userRoleModel()) -> getUserRoleList($user_group_id, 'user_group_id');
        return $userInfo ?
            message() -> success('获取用户权限成功', $userInfo) : message() -> nodata('暂无权限数据');
    }

    /**
     * 设置用户组权限
     * @param int $id
     * @param int $user_role_id
     * @return Response
     */
    #[PostMapping('set_user_group_role')]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function setUserGroupRole(int $id, int $user_role_id): Response {
        return (new userRoleModel()) -> setUserRoles($id, $user_role_id, 'user_group_id');
    }

    /**
     * 删除用户组权限
     * @param int $id
     * @param int $user_role_id
     * @return Response
     */
    #[DeleteMapping('delete_user_group_role/<[0-9]{1,8}:id/<[0-9]{1,8}:user_role_id>')]
    #[AuthAnnotation('userGroup', 'userGroupReadWrite|userGroupWrite')]
    public function deleteUserGroupRole(int $id, int $user_role_id): Response {
        return (new userRoleModel) -> deleteRole($id, $user_role_id, 'user_group_id');
    }

}